package com.website.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.website.service.IUserService;
import com.website.util.MessageUtil;
import com.website.util.SendUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


@Service("userService")
public class UserServiceImpl implements IUserService{ 
	private String domain="https://api.convertlab.com";
	private String appid="cl0393156f9765957";
	private String sercet="237966420371bd6dced52b8b7f507fa43065c297";
	private  static String customerstaticId = null;
	private  static String customerstaticeventtime = null;
	
	@Override
	public String addcustomer(Map<String, Object> paramap){
		String accessToken = getAccessToken();
		String url = domain + "/v1/customerandidentities?access_token=" + accessToken;
		JSONObject customer = getcustomer(paramap);
		String retunrstr = SendUtils.post(url, customer.toString());
		JSONObject returnobj = JSONObject.fromObject(retunrstr);
		System.out.println("创建客户返回的是" + returnobj);

		String customerId = returnobj.getString("id");
		customerstaticId = customerId; 
		String dateJoin = returnobj.getString("dateJoin");//这是用户创建的时间
		
		//开始推送事件
		Map eventnames = (Map)paramap.get("eventnames");
		String differentTime = "";
 		for (int i = 0; i < eventnames.size(); i++) {
            String b = i+"";
           String eventname = (String)eventnames.get(b);
			Map<String, Object> eventmap = new HashMap<String, Object>();
			eventmap.put("customerId", customerId); //客户id
			eventmap.put("accessToken", accessToken); 
			eventmap.put("eventname", eventname); //事件名称
			eventmap.put("eventtype", "open_page"); //事件类型
			 differentTime = MessageUtil.paserUtcTime(dateJoin,i);
			eventmap.put("dateJoin", differentTime); //用户创建时间
			  createCustomerEvent(eventmap);
		} 
 		customerstaticeventtime = differentTime;
 
		return customerId;
		
	}
	
	/**
	 * 获得accessToken
	 * @param appid
	 * @param sercet
	 */
	public String getAccessToken() {
		if (appid == null || "".equals(appid)) {
			return "";
		}
		
		if (sercet == null || "".equals(sercet)) {
			return "";
		}

		String url = domain + "/security/accesstoken";
		String retunrstr = SendUtils.sendGet(url,"grant_type=client_credentials&appid=" + appid + "&secret=" + sercet + "");
		// 发送get请求,通过appid和sercet获取accesstoken.
		// retunrstr ="{\"error_code\":0,\"access_token\":\"123\"}";
		JSONObject returnData = JSONObject.fromObject(retunrstr);
		System.out.println("调用accessToken" + returnData);
		String codes = returnData.get("error_code").toString();
		String access_token = "";
		if ("0".equals(codes)) {
			access_token = returnData.get("access_token").toString().replaceAll("\"", "");
		}
		return access_token;
	}
	
	/**
	 * 创建符合格式要求的客户json字符串
	 * @param openid
	 * @param unionid
	 */
	private JSONObject getcustomer(Map<String, Object> map) {
		String genderType = (String)map.get("genderType");
		if("男".equals(genderType)){
			genderType = "1";
		}else if("女".equals(genderType)){
			genderType = "2";
		}else{
			genderType = "0";
		}
		String familyType = (String)map.get("familyType");
		String familyrevenue = (String)map.get("familyrevenue");
		String cartype = (String)map.get("cartype");
		String  housetype= (String)map.get("housetype");
		JSONObject data = new JSONObject();
		JSONObject custidentitie = new JSONObject(); // 客户身份json
		JSONObject cust = new JSONObject(); // 客户json
		cust.put("gender", genderType);
		JSONObject attrdata = new JSONObject();
		attrdata.put("attr25",familyType );
		attrdata.put("attr26", familyrevenue);
		attrdata.put("attr27", cartype);
		attrdata.put("attr28", housetype);
		cust.put("attr", attrdata);
		JSONArray identitiesarr = new JSONArray();
		for (int i = 0; i < 1; i++) {
			custidentitie.put("identityType", "wechat");
			custidentitie.put("identityValue", "");
			custidentitie.put("identityName", "微信");
			identitiesarr.add(custidentitie);
		}

		data.put("customerIdentities", identitiesarr);
		data.put("customer", cust);
		return data;
	}
	
	/**
	 * 添加访问页面事件
	 * @param paramap
	 */
	private void createCustomerEvent(Map<String, Object> paramap) {
		String accessToken = (String) paramap.get("accessToken");
		String url = domain + "/v1/customerevents?access_token=" + accessToken;

		String customerId =(String) paramap.get("customerId");
		String dateJoin = (String) paramap.get("dateJoin");
		String eventname = (String) paramap.get("eventname");
		String eventtype = (String) paramap.get("eventtype");

		System.out.println("提交的事件是"+eventname+":"+dateJoin);
		JSONObject eventobj = new JSONObject();
		eventobj.put("customerId", customerId);
		eventobj.put("date", dateJoin);
		eventobj.put("event", eventtype);
		eventobj.put("targetName", eventname);
		SendUtils.post(url, eventobj.toString());
	}
    
	/**
	 * 添加保险事件
	 */
	@Override
	public String addInsurance(String targetname){
		String accessToken = getAccessToken();
		String url = domain + "/v1/customerevents?access_token=" + accessToken;
		String customerId = customerstaticId;
		String dateJoin = customerstaticeventtime;
		dateJoin = MessageUtil.paserUtcTime2(dateJoin);
		customerstaticeventtime = dateJoin;
		JSONObject eventobj = new JSONObject();
		eventobj.put("customerId", customerId);
		eventobj.put("date", dateJoin);
		eventobj.put("event", "click_link_in_page");
		eventobj.put("targetName", targetname);
		SendUtils.post(url, eventobj.toString());
		return null;
	}
}
